<?php
/**
 * Created by wzb.
 * User: wzb
 * time:2017/08/11
 */

require("DB.php");
ini_set("memory_limit","-1");
class Mazentop
{
/*
    public $token = "3roy94mue1mywxtqn4i5d0t5njqu7cim";
    //对接地址 非ssl 
    public $baseUrl = 'http://www.mslynnhair.com/rest/default/V1/';
    //获取token地址  
    public $tokenUrl = 'integration/admin/token';
    //API账号密码需要在后台创建
    public $userData = array('username' => 'api', 'password' => 'rymht123321');
    public $errorMsg = "";
*/
public $token;
public $baseUrl;
public $tokenUrl;
public $userData;
public $errorMsg;
public $username;
public $password;
//public $token_m;
//public $baseUrl_m;
//public $username_m;
//public $password_m;


    function __construct($token_m,$baseUrl_m,$username_m,$password_m)
    {
        
    $this->token = "$token_m";
    //对接地址 非ssl 
    $this->baseUrl = $baseUrl_m.'/rest/default/V1/';
    //获取token地址  
    $this->tokenUrl = 'integration/admin/token';
    //API账号密码需要在后台创建
    $this->userData = array('username' => "$username_m", 'password' => "$password_m");
    $this->errorMsg = "";
        try {
            $ch = curl_init($this->baseUrl . $this->tokenUrl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->userData));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            //curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type:application/json'
                )
            );
            $token = curl_exec($ch);
            //echo "$token_m,$baseUrl_m,$username_m,$password_m";
            $this->token = json_decode($token);
            curl_close($ch);
        } catch (Exception $ee) {
            $this->errorMsg = $ee->getMessage();
            die("exception:" . $this->errorMsg);
        }
    }
/**
* 订单接口 ： salesOrderRepositoryV1 GET /V1/orders
* method： get
* return ： data
**/
    public function getorders($pageSize,$currentPage)
    {
        echo "$pageSize,$currentPage\n";
		$datas = array(
		'searchCriteria[filterGroups][][filters][][field]' => '',
		'searchCriteria[filterGroups][][filters][][value]' => '',
		'searchCriteria[filterGroups][][filters][][conditionType]' => '',
		'searchCriteria[sortOrders][][field]' => '',
		'searchCriteria[sortOrders][][direction]' => '',
		'searchCriteria[pageSize]' => "$pageSize",//每页数量
		'searchCriteria[currentPage]' => "$currentPage"//当前页
		);
		$json_str = $this->sendPost($datas);
        $catagoryUrl = "orders?$json_str";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
		//print_r($result);
        return $result;
    }

/**
* 根据订单中的产品信息id ： salesOrderItemRepositoryV1 GET /V1/orders/items/{id}
* method： get
* return ： data
**/
    public function getproductAttribute($item_id)
    {
        $catagoryUrl = "orders/items/$item_id";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }

/**
* 根据attributeSetId 获取类目  catalogAttributeSetRepositoryV1 GET /V1/products/attribute-sets/{attributeSetId}
* method： get
* return ： data
**/
    public function getattributeSetId($attributeSetId)
    {
        $catagoryUrl = "products/attribute-sets/$attributeSetId";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }
/**
* 根据 sku ： catalogProductRepositoryV1 GET /V1/products/{sku} 获取产品信息
* method： get
* return ： data
**/
    public function getproductExtensionAttr($sku)
    {
        $catagoryUrl = "products/$sku";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }

/**
* 根据 attributecode： catalogProductAttributeOptionManagementV1 GET /V1/products/attributes/{attributeCode}/options
* method： get
* return ： data
**/
    public function getproductAttributeCode($attribute_code)
    {
        $catagoryUrl = "products/attributes/$attribute_code/options";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }
/**
* 获取产品： catalogProductRepositoryV1 GET GET /V1/products
* method： get
* return ： data
**/
    public function Getproduct($pageSize,$currentPage)
    {
        echo "$pageSize,$currentPage\n";
        $datas = array(
        'searchCriteria[filterGroups][][filters][][field]' => '',
        'searchCriteria[filterGroups][][filters][][value]' => '',
        'searchCriteria[filterGroups][][filters][][conditionType]' => '',
        'searchCriteria[sortOrders][][field]' => '',
        'searchCriteria[sortOrders][][direction]' => '',
        'searchCriteria[pageSize]' => "$pageSize",//每页数量
        'searchCriteria[currentPage]' => "$currentPage"//当前页
        );
        $json_str = $this->sendPost($datas);
        $catagoryUrl = "products?$json_str";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }









	/**
     * *
     * @param  封装参数
     * @return post_data
     */
	public function sendPost($datas) {
		$temps = array();	
		foreach ($datas as $key => $value) {
			if(!empty($value)){
			$temps[] = sprintf('%s=%s', $key, $value);
			}			
		}	
		$post_data = implode('&', $temps);
		return $post_data;
	}

}




 